package me.firstapp.repository.shop.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import me.firstapp.common.utils.DateUtils;
import me.firstapp.common.utils.Page;
import me.firstapp.common.utils.StrUtils;
import me.firstapp.module.shop.PointRechargeCard;
import me.firstapp.repository.base.BaseRepository;
import me.firstapp.repository.base.impl.EntityRepositoryImpl;
import me.firstapp.repository.shop.PointRechargeCardRepository;

@Repository
public class PointRechargeCardRepositoryImpl extends EntityRepositoryImpl<PointRechargeCard>
		implements PointRechargeCardRepository {
	
	@Autowired
	protected BaseRepository baseRepository;

	@Override
	protected Class<PointRechargeCard> getPersistentClass() {
		return PointRechargeCard.class;
	}

	@Override
	public PointRechargeCard findByCardNo(String cardNo) {
		StringBuilder sql = new StringBuilder(" FROM PointRechargeCard bean WHERE 1=1 ");
		Map<String, Object> parameters = new HashMap<>();

		sql.append(" AND bean.cardNo=:cardNo ");
		parameters.put("cardNo", cardNo);

		List<PointRechargeCard> result = super.query(PointRechargeCard.class, sql.toString(), parameters);

		return result.isEmpty() ? null : result.get(0);
	}

	@Override
	public Page<PointRechargeCard> findPointRechargeCardPage(Integer pageNo, Integer pageSize,
			Map<String, Object> searchParams, String sortParam, String sortType) {
		StringBuilder sql = new StringBuilder(" FROM PointRechargeCard bean where 1=1 ");
		Map<String, Object> parameters = new HashMap<>();

		if (searchParams != null) {
			Set<String> keys = searchParams.keySet();
			for (String key : keys) {
				Object value = searchParams.get(key);
				if (value != null) {
					if ("status".equals(key)) {
						String _value = (String) value;
						Integer status = Integer.valueOf(_value);
						sql.append(" AND bean.status =:status ");
						parameters.put("status", status);
					} else if ("useStatus".equals(key)) {
						String _value = (String) value;
						Integer useStatus = Integer.valueOf(_value);
						sql.append(" AND bean.useStatus =:useStatus ");
						parameters.put("useStatus", useStatus);
					} else if ("startDate".equals(key)) {
						String _value = (String) value;
						Date _startDate = DateUtils.getDateFromDateStr(_value, "yyyy-MM-dd");
						sql.append(" AND bean.startDate =:startDate ");
						parameters.put("startDate", _startDate);
					} else if ("endDate".equals(key)) {
						String _value = (String) value;
						Date _endDate = DateUtils.getDateFromDateStr(_value, "yyyy-MM-dd");
						sql.append(" AND bean.endDate =:endDate ");
						parameters.put("endDate", _endDate);
					} else {
						sql.append(" AND bean." + key + " LIKE :value ");
						parameters.put("value", "%" + value + "%");
					}
				}
			}
		}

		if (StrUtils.isNotNULL(sortParam) && StrUtils.isNotNULL(sortType)) {
			sql.append(" ORDER BY " + sortParam + " " + sortType);
		}

		return super.queryForPage(PointRechargeCard.class, sql.toString(), parameters, pageNo, pageSize);
	}

	@Override
	public Long findCardCountsByRechargeTime(Date startTime, Date endTime) {
		StringBuilder sql = new StringBuilder("SELECT COUNT(bean.id) FROM PointRechargeCard bean WHERE 1=1 ");
		Map<String, Object> parameters = new HashMap<>();

		Date _startTime = null;
		if (startTime != null) {
			String startTimeStr = DateUtils.getDateStr(startTime, "yyyy-MM-dd") + " 00:00:00";
			_startTime = DateUtils.getDateFromDateStr(startTimeStr, "yyyy-MM-dd HH:mm:ss");

		}
		Date _endTime = null;
		if (endTime != null) {
			String endTimeStr = DateUtils.getDateStr(endTime, "yyyy-MM-dd") + " 23:59:59";
			_endTime = DateUtils.getDateFromDateStr(endTimeStr, "yyyy-MM-dd HH:mm:ss");
		}

		if (startTime != null && endTime != null) {
			sql.append(" AND bean.rechargeTime >=:startTime ");
			parameters.put("startTime", _startTime);
			sql.append(" AND bean.rechargeTime <=:endTime ");
			parameters.put("endTime", _endTime);

		} else if (startTime != null && endTime == null) {
			sql.append(" AND bean.rechargeTime >=:startTime ");
			parameters.put("startTime", _startTime);
		} else if (startTime == null && endTime != null) {
			sql.append(" AND bean.rechargeTime <=:endTime ");
			parameters.put("endTime", _endTime);
		}
		sql.append(" AND bean.useStatus = 3 ");

		List<Long> result = super.query(Long.class, sql.toString(), parameters);
		return result.isEmpty() ? 0 : result.get(0);
	}

	@Override
	public Double findCardPointsByRechargeTime(Date startTime, Date endTime) {
		StringBuilder sql = new StringBuilder("SELECT SUM(pointValue) FROM PointRechargeCard as a WHERE 1=1 ");
		Map<String, Object> parameters = new HashMap<>();

		Date _startTime = null;
		if (startTime != null) {
			String startTimeStr = DateUtils.getDateStr(startTime, "yyyy-MM-dd") + " 00:00:00";
			_startTime = DateUtils.getDateFromDateStr(startTimeStr, "yyyy-MM-dd HH:mm:ss");

		}
		Date _endTime = null;
		if (endTime != null) {
			String endTimeStr = DateUtils.getDateStr(endTime, "yyyy-MM-dd") + " 23:59:59";
			_endTime = DateUtils.getDateFromDateStr(endTimeStr, "yyyy-MM-dd HH:mm:ss");
		}
		if (startTime != null && endTime != null) {
			sql.append(" AND rechargeTime >=:startTime ");
			parameters.put("startTime", _startTime);
			sql.append(" AND rechargeTime <=:endTime ");
			parameters.put("endTime", _endTime);
		} else if (startTime != null && endTime == null) {
			sql.append(" AND rechargeTime >=:startTime ");
			parameters.put("startTime", _startTime);
		} else if (startTime == null && endTime != null) {
			sql.append(" AND rechargeTime <=:endTime ");
			parameters.put("endTime", _endTime);
		}
		sql.append(" AND useStatus = 3 ");
		
		return baseRepository.queryForDouble(sql.toString(), parameters);
	}

}
